package com.hlbbcsr.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.hlbbcsr.business.AccountManager;
import com.hlbbcsr.domain.bean.Customer;
import com.hlbbcsr.domain.model.AccountModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author Ho.Viet.Duan Created Oct 28, 2013
 */
public class AccountAction extends ActionSupport implements
		ModelDriven<AccountModel> {

	private static final long serialVersionUID = 1L;
	private AccountModel model = new AccountModel();
	private AccountManager accountManager;

	public AccountModel getModel() {
		return model;
	}
	
	public void setAccountManager(AccountManager _accountManager) {
		accountManager = _accountManager;
	}	
	
	static final Logger log = Logger.getLogger(AccountAction.class);
	
	/**
	 * List Customer
	 */
	public String listCustomer() throws Exception {
		log.info("listCustomer:start");
		
		log.info("yeh this is to test Jenkins auto build");
		
		try {
			
			// get customer list from BUSINESS layer
			List<Customer> customerList = accountManager.listCustomer();
			
			// set customer-list into request to view in jsp
			model.setCustomerList(customerList);

		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		log.info("listCustomer:end");
		return SUCCESS;
	}
	
	/**
	 * Add Customer
	 */
	public String addCustomer() throws Exception {
		try {
			// Get customer information from screen (jsp)
			Customer customer = model.getCustomer();
			
			if (customer != null && StringUtils.isNotEmpty(customer.getLoginId())
								 && StringUtils.isNotEmpty(customer.getPassword())) {
				
				// Call business layer to add customer
				accountManager.addCustomer(customer);
			
			} else {
				return INPUT;
			}
			
		} catch (Exception e) {
			System.out.println(e);
			throw e;
		}
		return SUCCESS;
	}
}